<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">订单管理</a></li>
        <li class="active">用户订单</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="formSearch" class="form-horizontal form-search">
            <div class="form-group">
                <div class="col-md-3 col-sm-6">
                    <label class="control-label  col-xs-4" for="tradeNo">流水号:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" id="tradeNo">
                    </div>
                </div>
                <div class="col-md-3 col-sm-6">
                    <label class="control-label  col-xs-4" for="userName">用户姓名:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" id="userName">
                    </div>
                </div>
                <div class="col-md-3 col-sm-6">
                    <label class="control-label  col-xs-4" for="phone">手机号:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" id="phone">
                    </div>
                </div>
                <shiro:hasPermission name="food:list">
                    <div class="col-md-1 col-sm-6 " style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-info">查询</button>
                    </div>
                </shiro:hasPermission>

            </div>
        </form>
    </div>
    <div class="jax-box jax-box-table">
        <shiro:hasPermission name="order:batchComplete">
            <button id="btn_batch_complete" type="button" class="btn btn-danger">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量完成配送
            </button>
        </shiro:hasPermission>
        <table id="table"></table>
    </div>
</section>


<!-- 标签详情模态框 -->
<div id="dataDetailModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">订单信息</h4>
            </div>
            <div class="modal-body">
                <div id="dataOpenWindow">
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var completeFlag = "[[${@perms.hasPerm('order:complete')}]]";
    var editFlag = "[[${@perms.hasPerm('order:edit')}]]";
    var deleteFlag = "[[${@perms.hasPerm('order:delete')}]]";
    var columns = [
        {checkbox: true },
        {
            field: 'tradeNo',
            title: '流水号',
            align : "center"
        },{
            field: 'userName',
            title: '用户姓名',
            align : "center",
        },{
            field: 'money',
            title: '总金额(元)',
            align : "center",
        },{
            field: 'address',
            title: '配送地址',
            align : "center",
        },
        {
            field: 'phone',
            title: '联系方式',
            align : "center",
        },
        {
            field: 'status',
            title: '订单状态',
            align : "center",
            formatter : function(value,
                                 row, index) {
                var content = '已下单'
                switch (row.status) {
                    case 1:content='已下单';break;
                    case 2:content='已配送';break;
                }
                var text = '<label>'+content+'</label>'
                return text;
            }
        },
        {
            field: 'completeTime',
            title: '完成时间',
            align : "center",
            formatter : function(value,
                                 row, index) {
                var content = '未完成'
                if (row.completeTime) {
                    content = row.completeTime
                }
                var text = '<label>'+content+'</label>'
                return text;
            }
        },
        {
            field : 'operation',
            title : '操作',
            align : "center",
            class:'min-width-100',
            formatter : function(value,
                                 row, index) {
                var complete = (completeFlag=="true"&&row.status!=3)  ? '<a class="table-btn table-btn-info" onclick="completeData('+row.id+')">完成配送</a>' : '';
                var edit = editFlag=="true" ? '<a class="table-btn table-btn-info" onclick="editData('+row.id+')">查看</a>' : '';
                var del = deleteFlag=="true" ? '<a  class="table-btn table-btn-danger" onclick="deleteData('+row.id+')">删除</a>' : '';
                return complete+edit + del;
            }
        }];
    var options={
        id:"#table",
        url: '/order/list',
        columns:columns,
        toolbar: '#toolbar',
        showRefresh: true,
        queryParams : queryParams
    }
    Core.initTable(options);

    /*查询参数*/
    function queryParams(params) {
        console.log(params)
        var temp = { //这里的键的名字和控制器的变量名必须一致，这边改动，控制器也需要改成一样的
            pageNumber: params.pageNumber, // 页码
            pageSize: params.pageSize, // 分页大小
            tradeNo : $("#tradeNo").val(),
            userName : $("#userName").val(),
            phone : $("#phone").val()
        };
        return temp;
    }

    /*编辑*/
    function completeData(id) {
        Core.confirm("确定完成配送？",function () {
            Core.postAjax("/order/complete",{"id":id},function (data) {
                if(data.status==200){
                    Core.refreshTable("#table");
                }
                layer.msg(data.msg);
            })
        })
    }

    /*编辑*/
    function editData(id) {
        Core.load("#dataOpenWindow","/order/edit?id="+id,function(){
            $("#dataDetailModal").modal("show");
        },2);
    }
    /*删除*/
    function deleteData(id) {
        Core.confirm("确定删除？",function () {
            Core.postAjax("/order/delete",{"id":id},function (data) {
                if(data.status==200){
                    Core.refreshTable("#table");
                }
                layer.msg(data.msg);
            })
        })
    }
    $(function () {
        /*查询*/
        $("#btn_query").click(function(){
            Core.refreshTable("#table");
        });
        /*新增*/
        $("#btn_add").click(function(){
            Core.clearError("#dataForm");
        });

        /*批量删除*/
        $("#btn_batch_complete").click(function(){
            var checkedRows= Core.selectMutiData("#table");
            if(checkedRows){
                Core.confirm("确定完成配送选中的"+checkedRows.length+"条记录？",function () {
                    var ids=[];
                    $.each(checkedRows, function (i, item) {
                        ids.push(item.id);
                    })
                    Core.postAjax("/order/batch/complete", {"ids": ids}, function (data) {
                        if (data.status == 200) {
                            Core.refreshTable("#table");
                        }
                        layer.msg(data.msg);
                    })
                })
            }
        });


    });

</script>